/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package inovacao.modelos;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Giovani
 */
public class PagingModel extends DefaultTableModel {

    protected int pageSize;
    protected int pageOffset;
   private boolean listenerCamposTela;

    public boolean isListenerCamposTela() {
        return listenerCamposTela;
    }

    public void setListenerCamposTela(boolean listenerCamposTela) {
        this.listenerCamposTela = listenerCamposTela;
    }
   
    public String[] getCabecalho() {
        return cabecalho;
    }

    public void setCabecalho(String[] cabecalho) {
        this.cabecalho = cabecalho;
    }
    private boolean editavel;
    private String[] cabecalho;
    
    public PagingModel(String[] cabecalho) {
        super();
        this.editavel = false;
        this.cabecalho = cabecalho;
        this.listenerCamposTela = true;
        this.setColumnIdentifiers(this.cabecalho);

    }

    public boolean isEditavel() {
        return editavel;
    }

    public void setEditavel(boolean editavel) {
        this.editavel = editavel;
    }

    public PagingModel() {
        this(10000, 100);              
    }

    public PagingModel(int numRows, int size) {

        pageSize = size;
    }

    public int getPageOffset() {
        return pageOffset;
    }

    public int getPageCount() {
        return (int) Math.ceil((double) this.getRowCount() / pageSize);
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int s) {
        if (s == pageSize) {
            return;
        }
        int oldPageSize = pageSize;
        pageSize = s;
        pageOffset = (oldPageSize * pageOffset) / pageSize;
        fireTableDataChanged();
    }

    public void pageDown() {
        if (pageOffset < getPageCount() - 1) {
            pageOffset++;
            fireTableDataChanged();
        }
    }

    public void pageUp() {
        if (pageOffset > 0) {
            pageOffset--;
            fireTableDataChanged();
        }
    }

    public static JScrollPane createPagingScrollPaneForTable(JTable jt) {
        JScrollPane jsp = new JScrollPane(jt);
        TableModel tmodel = jt.getModel();
        if (!(tmodel instanceof PagingModel)) {
            return jsp;
        }

        final PagingModel model = (PagingModel) tmodel;
        final JButton upButton = new JButton("UP");
        upButton.setEnabled(false);
        final JButton downButton = new JButton("DOWN");
        if (model.getPageCount() <= 1) {
            downButton.setEnabled(false);
        }

        upButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                model.pageUp();

                if (model.getPageOffset() == 0) {
                    upButton.setEnabled(false);
                }
                downButton.setEnabled(true);
            }
        });

        downButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                model.pageDown();
                if (model.getPageOffset() == (model.getPageCount() - 1)) {
                    downButton.setEnabled(false);
                }
                upButton.setEnabled(true);
            }
        });

        jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

        jsp.setCorner(ScrollPaneConstants.UPPER_RIGHT_CORNER, upButton);
        jsp.setCorner(ScrollPaneConstants.LOWER_RIGHT_CORNER, downButton);



        return jsp;
    }

    @Override
    public boolean isCellEditable(int row, int col) {
        if (col == 0) {
            return false;
        } else if (this.editavel) {
            return true;
        } else {
            return false;
        }
    }

    void setData(String[][] string) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
